# -*- coding: utf-8 -*-
from utils.config import *
from utils.utils import *
import pandas as pd
import utils.MyTT as Mytt
# import logging
from utils.log import Loggers
from utils.pushService import PushService

logger = Loggers('MovingAverage')
# logger.clear()

pushSer = PushService('均线突破策略')

# 循环处理
for instId in config_movingAverage_instId:
    # 读取300条最新的数据
    df = loadCsvData(instId,'4h')

    # 计算指标 ema
    df['ema12'] = Mytt.EMA(df['close'],12)
    df['cross_ema'] = Mytt.CROSS(df['close'],df['ema12'])


    # 计算连阳
    df['zhang_num'] = Mytt.BARSLASTCOUNT(df['close'] > df['open'])
    df['die_num'] = Mytt.BARSLASTCOUNT(df['close'] < df['open'])

    # 寻找金叉+连阳的信号
    df['signal'] = 0
    df.loc[(df['zhang_num']>=3) & (df['cross_ema'].shift(2) ==1),'signal'] = 1
    df.loc[(df['die_num']>=3) & (df['cross_ema'].shift(2) ==-1),'signal'] = -1

    # 快速计算信号近期胜率
    shenglv5,shenglv10 = getShenglv(df)

    # 取出所有满足条件的信号数据
    df_signal = df[df['signal']!=0]
    # print(len(df_signal))
    # exit()
    # 拿到最后出现信号的数据
    after_signal = df_signal.iloc[-1]
    
    fangxiang = '做多' if after_signal['signal']>0 else '做空'
    hour = getHoursPassed(after_signal.name)
    signal_text = f"{instId}{fangxiang} 最近信号：{after_signal.name} 收盘价：{after_signal['close']} 5日胜率：{shenglv5}% 10日胜率：{shenglv10}%"

    # 进行日志记录
    if hour < 8:
        # 8小时内的新信号 就可以 进行钉钉推送
        if pushSer.add_push_item(after_signal, instId,[
            f"(5日胜率:{shenglv5}%)",
            f"(10日胜率:{shenglv10}%)",
        ]):
            logger.info(signal_text)

    # 完成消息的通知
pushSer.push_message()

